package com.baiyang.app.ui.bookCity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.databinding.FragmentBookCityBinding;
import com.baiyang.app.tool.LoadDataScrollController;
import com.baiyang.app.ui.read.ReadPageActivity;

import java.util.ArrayList;
import java.util.List;


public class BookCityFragment extends Fragment implements LoadDataScrollController.OnRecycleRefreshListener{

    private BookCityViewModel bookCityViewModel;
    private BookCityService bookCityService;
    private BookCityAdapter bookCityAdapter;
    private RecyclerView recyclerView;
    private FragmentBookCityBinding binding;
    private SwipeRefreshLayout mSwipeRefresh;
    private LoadDataScrollController mController;
    private ProgressDialog pd;

    /**
     * 并非创建时执行该方法
      * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentBookCityBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        bookCityViewModel =
                new ViewModelProvider(this).get(BookCityViewModel.class);
        bookCityService = new BookCityService(getContext(),bookCityViewModel);

        recyclerView = binding.bookCityRecyclerView;
        this.mController = new LoadDataScrollController(this);
        mSwipeRefresh = binding.refresh;
        //设置垂直的线性布局管理器，Orientation --> VERTICAL:垂直 HORIZONTAL:水平
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        //添加分割线
//        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        this.mSwipeRefresh.setColorSchemeColors(Color.RED,Color.GREEN,Color.BLUE);
        this.mSwipeRefresh.setOnRefreshListener(mController);
        this.recyclerView.addOnScrollListener(mController);
        initHandler();
        return root;
    }

    private void initHandler() {
        bookCityViewModel.getGetDataResult().observe(getViewLifecycleOwner(), new Observer<JSONObject>() {
            @Override
            public void onChanged(JSONObject jsonObject) {
                String result = jsonObject.getString("result");
                if("SUCCESS".equals(result)){
                    JSONArray array = jsonObject.getJSONArray("list");
                    List<JSONObject> list = JSONArray.parseArray(array.toJSONString(),JSONObject.class);
                    bookCityAdapter.addAll(list);
                }else{
                    String detail = jsonObject.getString("detail");
                    Toast.makeText(getContext(),detail,Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        bookCityAdapter = new BookCityAdapter(getContext(),new ArrayList<>());
        recyclerView.setAdapter(bookCityAdapter);
        bookCityAdapter.clear();
        bookCityService.getData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
        recyclerView.setAdapter(null);
        bookCityAdapter = null;
    }

    @Override
    public void refresh() {
        bookCityAdapter.clear();
        bookCityService.getData();
        mSwipeRefresh.setRefreshing(false);
        mController.setLoadDataStatus(false);
    }

    @Override
    public void loadMore() {

    }
}